﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.ViewModel;
using System.Collections.Generic;
using System.Linq;

namespace WeiboPlus.Shell.ViewModel
{
    public abstract class ViewModelBase<TItem> : NotificationObject
    {
        #region Properties
        bool _isCachedDataLoad;

        protected bool IsCachedDataLoad
        {
            get { return _isCachedDataLoad; }
            set { _isCachedDataLoad = value; RaisePropertyChanged(() => IsCachedDataLoad); }
        }

        bool _loading = false;
        public bool Loading
        {
            get { return _loading; }
            protected set
            {
                if (_loading != value)
                {
                    _loading = value;

                    RaisePropertyChanged("Loading");
                }
            }
        }

        TItem _item;

        public TItem Item
        {
            get { return _item; }
            set { _item = value; RaisePropertyChanged(() => Item); }
        }
        #endregion

        public ViewModelBase()
        {
            
        }

        public ViewModelBase(TItem t):this()
        {
            Item = t;
            Initialize();
        }

        public void ExecuteOnUIThread(Action action)
        {
            var option = Deployment.Current.Dispatcher.BeginInvoke(action);
        }

        public void ExecuteOnUIThread<T>(Action<T> action,T t)
        {
            var option = Deployment.Current.Dispatcher.BeginInvoke(action,t);
        }

        protected virtual void Initialize()
        {
        }

        protected virtual void LoadData()
        {
        }


        protected virtual void RefreshData()
        {
            //throw new NotImplementedException();
        }

        protected override void RaisePropertyChanged(string propertyName)
        {

            ExecuteOnUIThread(() =>
            {
                base.RaisePropertyChanged(propertyName);
            });
             
        }

    }
}
